package com.lvmama.pangolin.producthandler;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.github.pagehelper.PageInfo;
import com.lvmama.pangolin.common.Constant;
import com.lvmama.pangolin.common.StringTools;
import com.lvmama.pangolin.conf.ConfigManager;
import com.lvmama.pangolin.entity.tc.TCProductItem;
import com.lvmama.pangolin.entity.tc.TCommodityItem;
import com.lvmama.pangolin.tc.service.TCCommodityItemsService;
import com.lvmama.pangolin.tc.service.TCProductItemsService;

@Component("tcTicketProductParamHandel")
public class TCTicketProductParamHandel {
	/**
	* @author zhoutianqi
	* @className TCTicketProductParamHandel.java
	* @date 2017年6月6日 下午12:19:53
	* @description 
	*/
	private Logger logger = Logger.getLogger(getClass());
	@Autowired
	private ConfigManager configManager;
	@Autowired
	private TCProductItemsService productItemsService;
	@Autowired
	private TCCommodityItemsService commodityItemsService;
	@Value("#{'${product_page_size}'}")
	private int productPageSize;
	//所有打包票商品类型
	private String packageCommType = "";
	@Async
	public void executeProcess(){
		//查询的页数，默认查询第一页
		int page = 1;
		//总页数
		int totalPage = 0;
		//所有打包票商品类型
		try {
			packageCommType = configManager.getParamValue(Constant.TICKET_COMMODITY_STARDARD_TYPE_CONFIG, Constant.TICKET_COMMODITY_STARDARD_TYPE_CONFIG);
			List<TCProductItem> productList = productItemsService.selectByLimitNum(page, productPageSize);
			//当第一页没数据，表为空。当然一般不可能
			if(productList==null){
				logger.info("##############  TC产品处理线程处理完毕：");
				return;
			}
			PageInfo<TCProductItem> pageInfo = new PageInfo<TCProductItem>(productList);
			totalPage = pageInfo.getPages();
			// handle the first page data
			handelProduct(productList);
			//如果只有一页，不再作处理
			if(totalPage==1){
				logger.info("##############  TC产品处理线程处理完毕：");
				return;
			}
			for(int i=2;i<=totalPage;i++){
				List<TCProductItem> productList1 = productItemsService.selectByLimitNum(i, productPageSize);
				handelProduct(productList1);
			}
		} catch (Exception e) {
			logger.error("TC产品处理线程异常："+e.getMessage());
		}
		logger.info("##############  TC产品处理线程处理完毕：");
	}
	
	/**
	 * 处理产品字段逻辑
	*/
	public void handelProduct(List<TCProductItem> productList){
		for(TCProductItem item : productList){
			//优惠商品个数
			int commPreferentialNum = 0;
			//打包商品个数
			int commPackageNum = 0;
			List<TCommodityItem> commList = commodityItemsService.selectByProdId(item.getProduct_id());
			//设置产品对应商品的数量
			item.setCommodity_num(commList.size());
			for(TCommodityItem commItem : commList){
				if(StringTools.isValid(commItem.getPreferential())){commPreferentialNum++;}
				if(packageCommType.contains(commItem.getCommodity_type())){commPackageNum++;}
			}
			item.setComm_preferential_num(commPreferentialNum);
			item.setComm_package_num(commPackageNum);
			productItemsService.updateByItem(item);
		}
	}
}
